<!doctype html>
<html>
<head>
    <title>订单编辑</title>
    <?php $this->load->module('admin/index/page_header');?>
</head>
<body>
<div class="wrap">
    <div id="home_toptip"></div>
    <div class="nav">
        <ul>
            <li><a href="<?php echo $index_url;?>">酒店订单</a></li>
            <li class="current"><a href="javascript:;">编辑订单</a></li>
        </ul>
    </div>
    <div class="table_full">
        <div class="nav">
            <ul class="cc tab" id="js-config-menu">
                <li data-type="base_conf" class="current"><a href="javascript:void(0);">基本信息</a></li>
                <?php if(in_array($order_info['order_status'],array(4,5,6))):?>
                <li data-type="room_conf"><a href="javascript:void(0);">安排房间</a></li>
                <?php endif;?>
                <li data-type="goods_conf"><a href="javascript:void(0);">菜品管理</a></li>
                <li data-type="drinks_conf" ><a href="javascript:void(0);">酒水管理</a></li>
                <li data-type="logs_conf"><a href="javascript:void(0);">订单日志</a></li>
            </ul>
        </div>



        <form class="formvalidate" action="<?php echo $form_url;?>" method="post">

            <div id="base_config" class="js-config-warp">
            <?php $this->load->view('admin/hotel/tadmin/edit_base');?>
            </div>


            <div id="goods_config" class="js-config-warp" style="display: none" >
            <?php $this->load->view('admin/hotel/tadmin/edit_goods');?>
            </div>


            <div id="drinks_config" class="js-config-warp" style="display: none" >
                <?php $this->load->view('admin/hotel/tadmin/edit_drinks');?>
            </div>
          




            <?php if(in_array($order_info['order_status'],array(4,5,6))):?>
            <div id="room_config" class="js-config-warp" style="display: none" >
            <?php $this->load->view('admin/hotel/tadmin/edit_main_room');?>
            </div>


            <?php endif;?>

            <div id="logs_config" class="js-config-warp" style="display: none" >
                <?php $this->load->view('admin/hotel/tadmin/edit_logs');?>
            </div>

            <div class="h_a">订单价格信息</div>
            <div class="table_full">
                <table style="text-align: right" width="100%">
                    <tbody>
                    <?php if(in_array($order_info['order_status'],array(1,2))):?>
                    <tr>
                        <td>
                            定金：￥<input type="number" class="input rounded w100" name="earnest_account" value="<?php echo $order_info['earnest_account'];?>" id="input-earnest-account" validate="{required:true,number:true,min:0.01}" />元
                        </td>
                    </tr>
                    <?php endif;?>

                    <?php if(in_array($order_info['order_status'],array(4,5,6))):?>
                        <tr>
                            <td>
                                定金：￥<?php echo $order_info['earnest_account'];?>元
                            </td>
                        </tr>
                    <?php endif;?>


                    <tr>
                        <td>
                            房间金额：￥
                            <span class="js-span-room-account"><?php echo  $order_info['room_account'] ;?></span>元
                            <input name="room_account" id="js-input-room-account"  type="hidden"  value="<?php echo  $order_room_account ;?>">
                        </td>
                    </tr>


                    <tr>
                        <td>
                            +酒水和菜品金额：￥<span id="goods_price">0.00</span>元
                        </td>
                    </tr>

                    <tr>
                        <td>= 订单总金额：￥ <input type="number" class="input rounded w100" name="order_account" id="input-total-order-account" validate="{required:true,number:true}" value="<?php echo $order_info['order_account'];?>"  /> 元</td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <input type="hidden" name="order_id" value="<?php echo $order_info['order_id'];?>">

            <?php if($_GET['is_index'] == '1'):?>
                <input type="hidden" name="return_url" value="<?php echo site_url('order/adminhotel/static_list');?>">
            <?php endif;?>
            
            <input type="hidden" id="js-input-order-status" name="order_status" value="<?php echo $order_info['order_status'];?>">
            <input type="hidden"  name="debug" value="1">
            <input type="hidden"  name="conflict" value="-1">

            <?php if(!in_array($order_info['order_status'],array(6))):?>
            <div class="btn_wrap" >
                <div class="btn_wrap_pd">

                    <?php if($order_info['order_status'] != 3):?>
                    <button type="submit" class="btn btn_submit js-submit-btn  J_ajax_submit_btn" >修改订单</button>
                    <?php endif;?>
                    <?php if($order_info['order_status'] == 1):?>
                        <button type="button" class="btn btn_success js-operate-button" data-value="2" >审核通过</button>
                    <?php endif;?>

                    <?php if($order_info['order_status'] == 2):?>
                    <button type="button" class="btn btn_success js-operate-button" data-value="4" >确认订单</button>
                    <?php endif;?>

                    <?php if($order_info['order_status'] == 4 && $hotel_order_info['start_date'] <= date('Y-m-d')) :?>
                        <button type="button" class="btn   btn_success js-operate-button" data-value="5" >操作已入住</button>
                    <?php endif;?>

                    <?php if($order_info['order_status'] == 5 && $hotel_order_info['end_date'] <= date('Y-m-d')):?>
                        <button type="button" class="btn  btn_success js-operate-button" data-value="6" >完成订单</button>
                    <?php endif;?>

                    <?php if($order_info['order_status'] != 3):?>
                        <button type="button" class="btn  btn_error js-operate-button" data-value="3" >取消订单</button>
                    <?php endif;?>
                    <button type="button" class="btn btn_other" id="js-return-btn" onclick="history.go(-1)">返回</button>
                </div>
            </div>
                <?php else:?>
                <div class="btn_wrap" >
                    <div class="btn_wrap_pd">

                     <button type="button" class="btn btn_other" id="js-return-btn" onclick="history.go(-1)">返回</button>

                    </div>
                </div>

            <?php endif;?>
        </form>
    </div>
</div>

<script type="text/javascript">
    var urlD = '<?php echo $current_url;?>';
    var days = '<?php echo $days;?>';
    var uprice = '<?php echo $book_unit_price ;?>';
    var checkRoom = false;

    var orderId = '<?php echo $order_info['order_id'];?>';
    var startDate = '<?php echo $start;?>';
    var endDate = '<?php echo $end;?>';

    var totalMarketNumbers = <?php echo $hotel_order_info['number'];?> ;

    <?php if(in_array($order_info['order_status'],array(4,5))):?>
    checkRoom = true; // 验证房间必填
    <?php endif;?>


    $(function(){




        $('.js-input-room-check').click(function () {

            var $input = $(this).find('input');
            if($input.is(':disabled'))
            {
                $.dialog.tips('已入住的房间不可选');
                return false;
            }

        })

        $('.js-input-room-check').find('td:eq(0)').click(function () {
            var $input = $(this).find('input');
            $input.trigger('click');

        })

        $('.js-input-room-check').find('input').click(function () {

            $(this).trigger('click');
        })



        // 检测房间是否选择

        $.extend({fn_check_rooom_ini:function () {

            var check = false;
            $('#js-input-check-box').find('input[type="checkbox"]').each(function () {

                if($(this).is(':checked')&&!$(this).is(':disabled')){
                    check = true;
                    return false ;
                }
            })

            if(!check) {
                $.fn_loca_se_room('未选择任何房间');
                return false ;
            }
            return true; // 已选择房间
        }})




        // 检测房间日期行程冲突

        $.extend({fn_check_orders_conflict:function () {

            var conflict = true;
            var rids = new Array();
            $('#js-input-check-box').find('input[type="checkbox"]').each(function () {

                if ($(this).is(':checked') && !$(this).is(':disabled')) {
                    rids.push($(this).val());
                }
            })

            if (rids.length > 0) {

                $.ajax({
                    url: '<?php echo site_url('order/adminhotel/check_orders_conflict')?>',
                    cache: false,
                    async: false,
                    dataType: 'json',
                    type: 'POST',
                    data: {
                        order_id: orderId,
                        start: startDate,
                        end: endDate,
                        rooms: rids.join(',')
                    },
                    success: function (data) {
                        if (data.status == 0) {
                            $.dialog.tips(data.info);
                            conflict = false;
                        }
                    }
                });

            }

            return conflict;

        }})



        // 计算订单人数

        $.extend({
            calculate_numbers:function(){
                var numbers = Number($('#male_number').val()) + Number($('#woman_number').val()) + Number($('#child_number').val())
                $('.js-numbers').empty().text(numbers);
                totalMarketNumbers  = numbers ;
                return numbers ;
            }
        })



        // 计算房间输入入住人数

        $.extend({
            calculate_in_room_numbers:function(){
                var total = 0;
                $('#js-input-check-box').find('input[type="text"]').each(function () {

                    var $input_check = $(this).closest('.js-input-room-check').find('input[type="checkbox"]');
                    if(!$input_check.is(':disabled') && $input_check.is(':checked'))
                    {
                        total += Number($(this).val());
                    }
                })

                return total ;

            }
        })


        // 检测勾选房间 但是房间没有安排人数
        $.extend({
            check_room_no_numbers: function () {

                var check = true ;
                $('.js-input-room-check').find('input[type="checkbox"]').each(function () {

                    var $input_text = $(this).closest('.js-input-room-check').find('input[type="text"]');
                    if ($(this).is(':checked') && !$(this).is(':disabled')) {
                        if ($input_text.val() == '0') {

                            var title = $input_text.attr('data-title');
                            $.dialog.tips('房间' + title + '未安排人数');
                            check = false ;
                        }
                    }
                })

                return check ;
            }
        })
        // 减少房间人数
        $('.js-market-minus').click(function () {


            // 提示先选择房间
            var $input_check = $(this).closest('.js-input-room-check').find('input[type="checkbox"]');


            var $input = $(this).siblings('input');

            if($input.is(':disabled')) return false;

            var ival = $input.val();

            if(ival !== '0'){

                ival -= 1;
                $input.val(ival);
            }

            // 计算订单价格
            $.calculate_account();

        })

        // 增加房间人数

        $('.js-market-plus').click(function () {


            // 提示先选择房间
            var $input_check = $(this).closest('.js-input-room-check').find('input[type="checkbox"]');
            if(!$input_check.is(':disabled') && !$input_check.is(':checked'))
            {
                $.dialog.tips('先选择房间');
                return false;
            }


            // 计算已经选择的人数
            var inRoomNumbers = $.calculate_in_room_numbers();
            if(inRoomNumbers > (totalMarketNumbers -1))
            {
                $.dialog.tips('安排房间的人数不能大于订单人数 *^_^* ');
                return false;
            }


            var $input = $(this).siblings('input');

            if($input.is(':disabled'))  {

                $.dialog.tips('已入住房间不可选');
                return false;
            }

            var ival = parseInt($input.val());

            ival += 1;
            $input.val(ival);


            // 计算订单价格
            $.calculate_account();
        })






        // 提示房间选择信息
        $.extend({fn_loca_se_room:function (string) {
            $.dialog.tips(string);
            $('#js-config-menu').find('li:eq(1)').trigger('click');
            $("html,body").animate({scrollTop:$("#js-input-check-box").offset().top-100},500);
        }})






        
        $('#js-config-menu').find('li').click(function () {

            $(this).addClass('current').siblings('li').removeClass('current');
            $('.js-config-warp').hide();
            var selector = $(this).data('type')+'ig';
            $('#'+selector).show();
        })


        $('.js-operate-button').click(function () {
            var status = $(this).data('value');
            $('#js-input-order-status').val(status);
            if(checkRoom && !$.fn_check_rooom_ini())  return false;
            $(".js-submit-btn").submit();
        })


        $(".btn_submit").click(function () {


            if ((checkRoom && !$.fn_check_rooom_ini()) || (!$.fn_check_orders_conflict())) {
                $('#js-config-menu').find('li:eq(1)').trigger('click');
                $("html,body").animate({scrollTop:$("#js-input-check-box").offset().top-100},500);
                return false;
            }

            if($.calculate_in_room_numbers() <  totalMarketNumbers)
            {
                var remainNumber = Number(totalMarketNumbers) - Number($.calculate_in_room_numbers());
                $.dialog.tips('还有'+remainNumber+'人未安排房间 !');
                return false;
            }
            if(!$.check_room_no_numbers()) {
                return false;
            }




            $('#js-config-menu').find('li:eq(0)').trigger('click');



        })


        // 请选择微信用户

        $('#js-user-id').select2({placeholder: "请选择微信用户",allowClear: true});





        $.extend({
            calculate_account:function(){


                // 订单总人数
                var numbers = Number($('#male_number').val()) + Number($('#woman_number').val()) + Number($('#child_number').val())
                $('.js-numbers').empty().text(numbers);


                // 商品总价
                var goods_account = 0 ;
                $('.goods_body').find('.js-item-good-input').each(function () {
                    goods_account   += Number($(this).val())*Number($(this).data('price'));
                })
                $('#goods_price').text(goods_account);



                var rids = new Array();
                $('#js-input-check-box').find('input[type="checkbox"]').each(function () {

                    var $input_text = $(this).closest('.js-input-room-check').find('input[type="text"]');
                    if ($(this).is(':checked') && !$(this).is(':disabled')) {
                        if ($input_text.val() != '0') {
                            rids.push($(this).val());
                        }
                    }
                })


                var markets = new Array();

                $('#js-input-check-box').find('input[type="text"]').each(function () {

                    var $input_check = $(this).closest('.js-input-room-check').find('input[type="checkbox"]');
                    if(!$input_check.is(':disabled') && $input_check.is(':checked'))
                    {
                        if ($(this).val() != '0') {
                            markets.push($(this).val());
                        }

                    }
                })


               $.ajax({
                    url: '<?php echo site_url('order/adminhotel/calc_independ_room_account_ajax')?>',
                    cache: false,

                    dataType: 'json',
                    type: 'POST',
                    data: {
                        order_id: orderId,
                        start: startDate,
                        end: endDate,
                        rids: rids.join(','),
                        numbers: markets.join(',')
                    },
                    success: function (data) {
                        if (data.status == 0) {
                            $.dialog.tips(data.info);
                        }

                        if (data.status == 1) {

                            var info = eval('(' + data.info + ')');

                            //房间金额
                            $('#js-input-room-account').val(info.data);
                            $('.js-span-room-account').text(info.data);

                            // 商品金额
                            $('#goods_price').text(goods_account);

                            //订单总价
                            $('#input-total-order-account').val(info.data+goods_account);


                        }
                    }
                });



            }
        })





        <?php if($change_check):?>
        // 页面初始化计算订单价格
        $.calculate_account();
        <?php endif;?>


        // 商品总价
        var goods_account = 0 ;
        $('.goods_body').find('.js-item-good-input').each(function () {
            goods_account   += Number($(this).val())*Number($(this).data('price'));
        })
        $('#goods_price').text(goods_account);





        $('.js-input-numbers').change(function () {

            if($.calculate_numbers() == 0) $(this).val(1);
            $('.js-span-child').text('儿童数 : '+Number($('#child_number').val())+'人');
            $('.js-span-male').text('成人男数 : '+Number($('#male_number').val())+'人');
            $('.js-span-woman').text('成人女数 : '+Number($('#woman_number').val())+'人');
//            $.calculate_account();
        })



        $('#js-user-id').change(function () {

            var uid = $(this).val();
            var name = $(this).find('option:selected').text();

            $('#js-contact-name').val('');
            $('#js-contact-phone-num').val('');

            if(uid !== '0') {

                $.post('<?php echo site_url('member/admin/ajax_memeber')?>',{
                    id:uid
                },function(data){
                    if(data.status=='1'){

                        var json = eval("("+data.info+")");
                        $('#js-contact-name').val(json['realname']);
                        $('#js-contact-phone-num').val(json['mobile']);
                    }
                },'json')

                $('#js-wx-nick').val(name);
            }
        })


        $('.js-minus').click(function () {

            var $input = $(this).siblings('input');

            var ival = $input.val();

            if(ival !== '0'){

                ival -= 1;
                $input.val(ival);
            }
            $.calculate_account();

        })

        $('.js-plus').click(function () {

            var $input = $(this).siblings('input');

            var ival = parseInt($input.val());

            ival += 1;
            $input.val(ival);
            $.calculate_account();
        })

        new Kalendae.Input('start_date', {
            months:1,
            mode:'single',
            format:'YYYY-MM-DD',
            direction:'today-future'
        });


        new Kalendae.Input('end_date', {
            months:1,
            mode:'single',
            format:'YYYY-MM-DD',
            direction:'future'
        });



        $('#start_date').change(function () {


            var date = $(this).val();
            var month = '';
            var day = '';
            d = new Date(date);
            d = +d + 1000*60*60*24;
            d = new Date(d);



            if((d.getMonth()+1).toString().length == 2){
                month = (d.getMonth()+1);
            }else {
                month = '0'+(d.getMonth()+1);
            }

            if(d.getDate().toString().length == 2){
                day = d.getDate();
            }else {
                day = '0'+(d.getDate());
            }

            var newdate = d.getFullYear()+"-"+month+"-"+day;




            $('#end_date').val(newdate);



            var end = $('#end_date').val();
            var df_date = $('#end_date').data('default');

            if(end !== df_date)
            {
                var url = urlD+'&start='+date+'&end='+end ;
                $.dialog.tips('加载中...');
                window.location.href = url;
            }


        })

        $('#end_date').change(function () {

            var start = $('#start_date').val();
            var end = $('#end_date').val();
            var url = urlD+'&start='+start+'&end='+end ;
            $.dialog.tips('加载中...');
            window.location.href = url;

        })




    })
</script>
</body>
</html>